package com.ipzoe.common.repository;

import com.ipzoe.common.bean.entity.AdminMenu;
import com.ipzoe.common.util.DefaultMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 菜单Repository
 * <p>
 * Created 2018/01/19.
 *
 * @author pfzhang
 */
@Mapper
public interface AdminMenuRepository extends DefaultMapper<AdminMenu> {

    /**
     * 根据level组织后台管理菜单并且去除首页管理.(获取除首页外的所有菜单)
     *
     * @param level  菜单层级
     * @param father 父菜单ID
     * @return 菜单集合
     */
    @Select("SELECT * FROM admin_menu WHERE id <> 1 and level = #{level} AND ifnull(father, 0) = #{father} and del_flag = 0")
    List<AdminMenu> findByLevelAndFather(@Param("level") int level, @Param("father") Long father);

    /**
     * 根据level组织后台管理菜单.(获取该角色下的菜单)
     *
     * @param roleId 角色ID
     * @param level  菜单层级
     * @param father 父菜单ID
     * @return 菜单集合
     */
    @Select("select root.* from admin_menu root inner join role_admin_menu ram " +
            " on root.id = ram.admin_menu_id and ram.del_flag = 0 " +
            " and ram.role_id = #{roleId} " +
            " where root.level = #{level} " +
            " and ifnull(root.father, 0) = #{father} " +
            " and root.del_flag = 0 " +
            " order by root.order ")
    List<AdminMenu> findByUserIdAndLevelAndFather(@Param("roleId") Long roleId,
                                                  @Param("level") Integer level,
                                                  @Param("father") Long father);

    /**
     * 获取所有的菜单集合,不包含首页
     *
     * @return 菜单集合
     */
    @Select("SELECT * FROM admin_menu WHERE id <> 1 and del_flag = 0")
    List<AdminMenu> selectAllNotIndex();
}
